package com.ruoyi.edh.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.edh.domain.EdhFollow;
import com.ruoyi.edh.domain.model.EdhFollowModel;
import com.ruoyi.edh.mapper.EdhFollowMapper;
import com.ruoyi.edh.service.IEdhFollowService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 易订货-学院关注Service业务层处理
 * 
 * @author xxx
 * @date 2020-09-02
 */
@Service
public class EdhFollowServiceImpl implements IEdhFollowService
{
    @Autowired
    private EdhFollowMapper edhFollowMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询易订货-学院关注
     * 
     * @param id 易订货-学院关注ID
     * @return 易订货-学院关注
     */
    @Override
    public EdhFollow selectEdhFollowById(Long id)
    {
        return edhFollowMapper.selectEdhFollowById(id);
    }

    /**
     * 查询易订货-学院关注列表
     * 
     * @param edhFollow 易订货-学院关注
     * @return 易订货-学院关注
     */
    @Override
    public List<EdhFollow> selectEdhFollowList(EdhFollow edhFollow)
    {
        Long me = SecurityUtils.getUserId();
        return edhFollowMapper.selectEdhFollowList(edhFollow,me);
    }

    /**
     * 查询用户关注与粉丝情况
     */
    @Override
    public Map<String,Object> followAndFans(Long userId) {


        Map<String,Object> result = new HashMap<String, Object>(16);

        Long me = SecurityUtils.getUserId();

        result.put("user",userMapper.selectEasyUser(userId));
        // 查看关注的人
        EdhFollow searchFollow = new EdhFollow();
        searchFollow.setCreateBy(userId.toString());
        List<EdhFollow> follows = edhFollowMapper.selectEdhFollowList(searchFollow,me);

        // 查看粉丝
        EdhFollow searchFans = new EdhFollow();
        searchFans.setUserId(userId);
        List<EdhFollow> fans = edhFollowMapper.selectEdhFansList(searchFans,me);

        result.put("followsCount",null == follows?0:follows.size());
        result.put("follows",follows);
        result.put("fansCount",null == fans?0:fans.size());
        result.put("fans",fans
         );

        return result;
    }

    /**
     * 学院推荐
     */
    @Override
    public List<Map<String,Object>> collegeRecommend() {
        Long userId = SecurityUtils.getUserId();
        return edhFollowMapper.selectCollegeRecommend(userId);
    }

    /**
     * 新增易订货-学院关注
     * 
     * @param edhFollow 易订货-学院关注
     * @return 结果
     */
    @Override
    public int insertEdhFollow(EdhFollowModel.Insert edhFollow)
    {
        Long userId = SecurityUtils.getUserId();

        EdhFollow insertFollow = new EdhFollow();
        insertFollow.setCreateBy(userId.toString());
        insertFollow.setUserId(edhFollow.getUserId());

        List<EdhFollow> edhFollows = edhFollowMapper.selectEdhFollowList(insertFollow,userId);
        if (null == edhFollows || 0 == edhFollows.size()){
            edhFollowMapper.insertEdhFollow(insertFollow);
            return  1 ;
        }else {
            edhFollowMapper.deleteEdhFollowById(edhFollows.get(0).getId());
            return 0;
        }

    }

    /**
     * 修改易订货-学院关注
     * 
     * @param edhFollow 易订货-学院关注
     * @return 结果
     */
    @Override
    public int updateEdhFollow(EdhFollow edhFollow)
    {
        return edhFollowMapper.updateEdhFollow(edhFollow);
    }

    /**
     * 批量删除易订货-学院关注
     * 
     * @param ids 需要删除的易订货-学院关注ID
     * @return 结果
     */
    @Override
    public int deleteEdhFollowByIds(Long[] ids)
    {
        return edhFollowMapper.deleteEdhFollowByIds(ids);
    }

    /**
     * 删除易订货-学院关注信息
     * 
     * @param id 易订货-学院关注ID
     * @return 结果
     */
    @Override
    public int deleteEdhFollowById(Long id)
    {
        return edhFollowMapper.deleteEdhFollowById(id);
    }
}
